package com.bw.ivideo.ui

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.drawable.Animatable
import android.os.Build
import android.os.CountDownTimer
import android.os.Debug
import android.os.Trace
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.bw.common.BaseApplication
import com.bw.common.common.ConstantValue
import com.bw.common.utils.SharedPreferenceDelegate
import com.bw.ivideo.MainActivity
import com.bw.ivideo.databinding.ActivitySplashBinding
import com.bw.mvicore.ui.BaseActivity

class SplashActivity : BaseActivity() {
    private lateinit var binding: ActivitySplashBinding

    /**
     * 是否首次启动
     */
    private var isFirst:Boolean by SharedPreferenceDelegate<Boolean>(
        BaseApplication.getApplicationContext(),
        ConstantValue.SP_NAME,
        ConstantValue.SP_KEY_FIRST,true)

    override fun releaseRes() {

    }

    override fun generateContentView(): View {
        binding = ActivitySplashBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        super.initView()


        Trace.beginSection("sss")
        Debug.startMethodTracing()

        if (checkPermissions()) {
            countDownStart()
        } else {
            ActivityCompat.requestPermissions(this, getRequiredPermissions(), PERMISSION_REQ_ID)
        }

        Debug.stopMethodTracing()
        Trace.endSection()

    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        Trace.endSection()
    }

    // 系统权限申请回调
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (checkPermissions()) {
            countDownStart()
        }
    }

    var n=0
    override fun initData() {
        if (isFirst){

        }
        isFirst=false
    }

    private lateinit var countDownTimer:CountDownTimer
    private fun countDownStart() {
        countDownTimer = object : CountDownTimer(5000L,1000L){
            override fun onTick(millisUntilFinished: Long) {
                ++n
                binding.tvSplashCountdown.text="倒计时:${(5-n).toString()}s"
            }

            override fun onFinish() {
                route(MainActivity::class.java)
                closeThiz()
            }

        }
        countDownTimer.start()
    }

    override fun initEvent() {
        val drawable = binding.ivSplashLogo.drawable
        if (drawable is Animatable){
            val animatable = drawable as Animatable
            animatable.start()
        }

        binding.tvSplashCountdown.setOnClickListener {
            route(MainActivity::class.java)
            closeThiz()
        }
    }


    private val PERMISSION_REQ_ID = 22

    // 获取体验实时音视频互动所需的录音、摄像头等权限
    private fun getRequiredPermissions(): Array<String> {
        // 判断 targetSDKVersion 31 及以上时所需的权限
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            arrayOf<String>(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.RECORD_AUDIO,  // 录音权限
                Manifest.permission.CAMERA,  // 摄像头权限
                Manifest.permission.READ_PHONE_STATE,  // 读取电话状态权限
                Manifest.permission.BLUETOOTH_CONNECT // 蓝牙连接权限
            )
        } else {
            arrayOf<String>(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
            )
        }
    }

    private fun checkPermissions(): Boolean {
        for (permission in getRequiredPermissions()) {
            val permissionCheck = ContextCompat.checkSelfPermission(this, permission)
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                return false
            }
        }
        return true
    }

    override fun onStop() {
        super.onStop()
        if (::countDownTimer.isInitialized){
            countDownTimer.cancel()
        }
    }
}